﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using FX.CC.Andon.Entity.ReportManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.ReportManage
{
    /// <summary>
    /// 异常柏拉图数据层
    /// </summary>
    public class ExceptionPlatoService : DbHelperBase
    {
        public List<ExceptionPlatoEntity> GetExceptionPlatoList(AbnormalQcEntity qcmodel)
        {
            var list = new List<ExceptionPlatoEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT COUNT(B.ETypeCode) number,E.ETypeName FROM  UT_EventRec AS B ");
            strSql.Append(" INNER JOIN UT_Equipment AS C ON B.EqID=C.EqID  ");
            strSql.Append(" INNER JOIN UT_Line AS D ON C.LineID=D.LineID ");
            strSql.Append(" INNER JOIN UT_EType AS E ON B.ETypeCode=E.ETypeCode ");
            strSql.Append(" WHERE B.EndTime IS NOT NULL AND B.IsCancle=0  ");
            if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
            {
                strSql.Append(" AND B.BizDate >=@SBizDate ");
            }
            if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
            {
                strSql.Append(" AND B.BizDate <=@EBizDate ");
            }
            if (!string.IsNullOrEmpty(qcmodel.EqID))
            {
                strSql.Append(" AND B.EqID='" + qcmodel.EqID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.LineID))
            {
                strSql.Append(" AND D.LineID ='" + qcmodel.LineID + "'");
            }
            strSql.Append(" GROUP BY E.ETypeName ORDER BY Number DESC");

            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
                    {
                        Db.AddInParameter(cmd, "@SBizDate", DbType.DateTime, Convert.ToDateTime(qcmodel.BizDateFrom));
                    }
                    if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
                    {
                        Db.AddInParameter(cmd, "@EBizDate", DbType.DateTime, Convert.ToDateTime(qcmodel.BizDateEnd));
                    }

                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new ExceptionPlatoEntity
                            {
                                Name =
                                    reader["ETypeName"] == obj
                                        ? null
                                        : Convert.ToString(reader["ETypeName"])
                            };
                            if (reader["Number"] != DBNull.Value)
                                model.Number = Convert.ToInt32(reader["Number"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
    }
}
